package com.kaifamiao.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author lucien-ma
 * @date 2021/8/27
 * @apiNote
 */
public class MyJDKProxy implements InvocationHandler {

    private Object obj = null;

    public Object bind(Object obj) {
        this.obj = obj;
        return Proxy.newProxyInstance(MyJDKProxy.class.getClassLoader(),
                obj.getClass().getInterfaces(),
                (InvocationHandler) this);
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("jdk 代理模式");
        System.out.println("执行了额外的扩展功能.....");
        Object result = method.invoke(obj, args);
        return result;
    }

    public static void main(String[] args) {
        MyJDKProxy myJDKProxy = new MyJDKProxy();
        IActor cl = (IActor) myJDKProxy.bind(new Actor("成龙"));
        cl.danger(6565.23);
    }
}
